// 引入PDF.js
import * as pdfjs from 'pdfjs-dist'
if (typeof Promise.withResolvers === 'undefined') {
  if (window)
    // @ts-expect-error This does not exist outside of polyfill which this is doing
    window.Promise.withResolvers = function () {
      let resolve, reject
      const promise = new Promise((res, rej) => {
        resolve = res
        reject = rej
      })
      return { promise, resolve, reject }
    }
}
pdfjs.GlobalWorkerOptions.workerSrc = new URL(
  'pdfjs-dist/legacy/build/pdf.worker.min.mjs',
  import.meta.url,
).toString()
import { PDFDocumentProxy } from 'pdfjs-dist/types/src/display/api'

import { ref, unref } from 'vue'
import type { UploadFile } from 'element-plus'
import { getBufferWithBlob } from '@/utils/file'
import {
  docTypes,
  excelTypes,
  htmlTypes,
  imgTypes,
  pptTypes,
  textTypes,
} from '@/constant/file.constant'
import { transType } from '@/types/file'
import type { FileType } from '@/types/file'
import { usePdf2Img } from './usePdf2Img'
import { usePdf2Text } from './usePdf2Text'
import { usePdf2Ppt } from './usePdf2Ppt'
import { usePdf2Word } from './usePdf2Word'
import { usePdf2Excel } from './usePdf2Excel'
import { usePdf2Html } from './usePdf2Html'

const { transferPDF2Img, saveImg } = usePdf2Img()
const { transferPDF2Text, saveText } = usePdf2Text()
const { transferPDF2Ppt, savePpt } = usePdf2Ppt()
const { transferPDF2Word, saveWord } = usePdf2Word()
const { transferPDF2Excel, saveExcel } = usePdf2Excel()
const { transferPDF2Html, saveHtml } = usePdf2Html()

export function useHandlePdf(fileType: FileType) {
  const fileRes = ref<UploadFile>()
  let pdf: PDFDocumentProxy
  const pdfBuffer = ref<ArrayBuffer>()

  /**
   * 加载PDF
   */
  const parsePDF = async () => {
    if (!unref(fileRes)) return
    pdfBuffer.value = await getBufferWithBlob(unref(fileRes)!.raw as Blob)
    const loadingTask = pdfjs.getDocument(unref(pdfBuffer))
    pdf = await loadingTask.promise
    return Promise.resolve()
  }

  /**
   * 转换PDF
   */
  const transferPDF = async () => {
    // ppt
    if (transType(pptTypes).includes(fileType)) {
      await transferPDF2Ppt(unref(pdf)!)
    }
    // word
    else if (transType(docTypes).includes(fileType)) {
      await transferPDF2Word(unref(pdf)!)
    }
    // html
    else if (transType(htmlTypes).includes(fileType)) {
      await transferPDF2Html(unref(pdf)!)
    }
    // excel
    else if (transType(excelTypes).includes(fileType)) {
      await transferPDF2Excel(unref(pdf)!)
    }
    // 文本
    else if (transType(textTypes).includes(fileType)) {
      await transferPDF2Text(unref(pdf)!)
    }
    // 图片
    else if (transType(imgTypes).includes(fileType)) {
      await transferPDF2Img(unref(pdf)!)
    }
    return Promise.resolve()
  }

  /**
   * 保存文件
   */
  const saveFile = async () => {
    const index = unref(fileRes)!.name.lastIndexOf('.')
    let fileName = unref(fileRes)!.name
    if (index !== -1) {
      fileName = fileName.slice(0, index)
    }
    // ppt
    if (transType(pptTypes).includes(fileType)) {
      await savePpt(`${fileName}${fileType}`)
    }
    // word
    else if (transType(docTypes).includes(fileType)) {
      await saveWord(`${fileName}${fileType}`)
    }
    // html
    else if (transType(htmlTypes).includes(fileType)) {
      await saveHtml(`${fileName}${fileType}`)
    }
    // excel
    else if (transType(excelTypes).includes(fileType)) {
      await saveExcel(`${fileName}${fileType}`)
    }
    // 文本
    else if (transType(textTypes).includes(fileType)) {
      saveText(fileName)
    }
    // 图片
    else if (transType(imgTypes).includes(fileType)) {
      saveImg(fileName, fileType)
    }
    return Promise.resolve()
  }

  return { fileRes, parsePDF, transferPDF, saveFile }
}
